package com.leilei.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * JSON转化工具类
 */
public class JSONUtil {

    private static ObjectMapper objectMapper = new ObjectMapper();

    public static String ObjectToJSONAsString(Object object){

        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T JSONToObject(String json, Class<T> tClass){
        try {
            return objectMapper.readValue(json, tClass);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
